﻿using System.Collections.Generic;
using System.Linq;
using Apps.Web.Core;
using Apps.IBLL.S;
using Apps.Locale;
using System.Web.Mvc;
using Apps.Common;
using Apps.IBLL;
using Apps.Models.S;
using Microsoft.Practices.Unity;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.Data;
using Apps.Models;
using Wantn.FIS.Enum;

namespace Apps.Web.Areas.S.Controllers
{
    public class SampleController : BaseController
    {
        [Dependency]
        public IS_SAMPLEBLL m_BLL { get; set; }
        ValidationErrors errors = new ValidationErrors();

        [SupportFilter]
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        [SupportFilter(ActionName = "Index")]
        public JsonResult GetList(GridPager pager, string batch,string rfid,string startcdate,string endcdate,
                string startdate,string enddate)
        {
            List<V_S_SAMPLE_RC> list = m_BLL.GetList_RC(ref pager, batch, rfid,  startcdate,  endcdate,
                 startdate,  enddate);
            GridRows<V_S_SAMPLE_RC> grs = new GridRows<V_S_SAMPLE_RC>();
            for(int i=0;i< list.Count;i++)
            {
                //如果是汽车采样机的数据，则不显示矿点名称
                if(!string.IsNullOrEmpty(list[i].TRANSPORT_ID))
                {
                    list[i].MINE_NAME = "";
                }
            }
            grs.rows = list;
            grs.total = pager.totalRows;
            return Json(grs);
        }

        #region 导出导入
        [HttpPost]
        [SupportFilter(ActionName = "Export")]
        public JsonResult CheckExportData(string batch, string rfid, string startcdate, string endcdate,
                string startdate, string enddate)
        {
            var list = m_BLL.GetList_RC(ref GridPager.NotPage, batch, rfid, startcdate, endcdate,
                 startdate, enddate);
            if (list.Count().Equals(0))
            {
                return Json(JsonHandler.CreateMessage(0, "没有可以导出的数据"));
            }
            else
            {
                return Json(JsonHandler.CreateMessage(1, "可以导出"));
            }
        }
        [SupportFilter]
        public ActionResult Export(string batch, string rfid, string startcdate, string endcdate,
                string startdate, string enddate)
        {
            List<V_S_SAMPLE_RC> list = m_BLL.GetList_RC(ref GridPager.NotPage,  batch, rfid, startcdate, endcdate,
                 startdate, enddate);
            JArray jObjects = new JArray();
            var enumDir = EnumMvcHelper.GetEnumHash<SampleType>();
            foreach (var item in list)
            {
                var jo = new JObject();
                jo.Add("批次号", item.BATCH_CODE);
                jo.Add("试样号", item.SAMPLE_CODE);
                jo.Add("物料", item.MATERIEL_NAME);
                jo.Add("矿点", item.MINE_NAME);
                jo.Add("供应商", item.SUPPLIER_NAME);
                jo.Add("运输商", item.TRANSPORT_NAME);
                jo.Add("样品类型", item.SAMPLE_TYPE_STR);
                jo.Add("RFID芯片号", item.RFID);
                jo.Add("样品生成时间", item.CREATE_TIME);
                jo.Add("样品称重员", item.WEIGHT_USER_NAME);
                jo.Add("样品称重时间", item.WEIGHT_TIME);
                jo.Add("样品重量(G)", item.WEIGHT);
                jObjects.Add(jo);
            }
            var dt = JsonConvert.DeserializeObject<DataTable>(jObjects.ToString());
            var exportFileName = string.Concat(
                "试样信息",
                DateTime.Now.ToString("yyyyMMdd"),
                ".xlsx");
            return new ExportExcelResult
            {
                SheetName = "Sheet1",
                FileName = exportFileName,
                ExportData = dt
            };
        }
        #endregion
    }
}
